package com.example.demo.controller;

import com.example.demo.domain.User;
import com.example.demo.domain.Bicycle;

import java.util.*;

import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/second-hand")     // 通过这里配置使下面的映射都在/sencond-hand下，可去除
public class UserController {

    static Map<String, User> users = Collections.synchronizedMap(new HashMap<>());
    static Map<String, Bicycle> bicycles = Collections.synchronizedMap(new HashMap<>());

    @ApiOperation(value = "获取用户列表", notes = "")
    @RequestMapping(value = {"/{users}"}, method = RequestMethod.GET)
    public List<User> getUserList() {
        return new ArrayList<>(users.values());

    }

    @ApiOperation(value = "创建用户", notes = "根据User对象创建用户")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    @RequestMapping(value = "/{users}", method = RequestMethod.POST)
    public String postUser(@RequestBody User user) {
        users.put(user.getName(), user);
        return "success";
    }

    @ApiOperation(value = "获取用户详细信息", notes = "根据url的id来获取用户详细信息")
    @ApiImplicitParam(name = "name", value = "用户姓名", required = true, dataType = "String")
    @RequestMapping(value = "/{users}/{name}", method = RequestMethod.GET)
    public User getUser(@PathVariable String name) {
        return users.get(name);
    }


    @ApiOperation(value = "删除用户", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(name = "name", value = "用户姓名", required = true, dataType = "String")
    @RequestMapping(value = "/{users}/{name}", method = RequestMethod.DELETE)
    public String deleteUser(@PathVariable String name) {
        users.remove(name);
        return "success";
    }

    @ApiOperation(value = "获取在售电驴列表", notes = "")
    @RequestMapping(value = {"/{bicycles}"}, method = RequestMethod.GET)
    public List<Bicycle> getBicycleList() {
        return new ArrayList<>(bicycles.values());

    }

    @ApiOperation(value = "创建电驴信息", notes = "根据Bicycle对象创建电驴")
    @ApiImplicitParam(name = "bicycle", value = "电驴详细实体bicycle", required = true, dataType = "Bicycle")
    @RequestMapping(value = "/{bicycles}", method = RequestMethod.POST)
    public String postBicycle(@RequestBody Bicycle bicycle) {
        bicycles.put(bicycle.getType(), bicycle);
        return "success";
    }

    @ApiOperation(value = "获取电驴详细信息", notes = "根据url的type来获取电驴详细信息")
    @ApiImplicitParam(name = "type", value = "型号", required = true, dataType = "String")
    @RequestMapping(value = "/{bicycles}/{type}", method = RequestMethod.GET)
    public Bicycle getBicycle(@PathVariable String type) {
        return bicycles.get(type);
    }


    @ApiOperation(value = "删除电驴", notes = "根据url的type来指定删除对象")
    @ApiImplicitParam(name = "type", value = "型号", required = true, dataType = "String")
    @RequestMapping(value = "/{bicycles}/{type}", method = RequestMethod.DELETE)
    public String deleteBook(@PathVariable String type) {
        bicycles.remove(type);
        return "success";
    }


}